<?php
namespace app\index\controller;
use catetree\Catetree;
class Flow extends Base
{
    //添加购物车
    public function addToCart(){
        $data=input('post.');
        $goods=$data['goods'];
        $goods=json_decode($goods);
        $goods_id=$goods->goods_id;
        $goodsAttr_id=$goods->goodsAttrIds;
        $goodsNum=$goods->number;
        model('TpCart')->addToCart($goods_id,$goodsAttr_id,$goodsNum);

        $result=[
        'error'=>0,
        'one_step_buy'=>1,
        ];//error=0说明加入购物车成，库存没问题，error=2说明库存不足，未加入购物车
        return json($result);
    }


    public function flow1(){
        $cartRes=model('TpCart')->getGoodsListInCart();
        // dump($cartRes);die;
        $this->assign([
            'cartRes'=>$cartRes,
        ]);
        return view();
    }


    public function flow2(){
        $data=input('post.');
        $uid= session('uid');
        if(!$uid){
            $this->error('请先登录',url('member/Account/login'));
        }
        //收货地址信息
        $address=db('tp_address')->where('user_id',$uid)->find();//如果存在就更新，不存在就插入
        $this->assign('address',$address);
        if($data){
        $recId=$data['cart_value'];//选中商品key的字符串
        $payGoodsRes=model('TpCart')->payGoodsInfo($recId);
//        dump($payGoodsRes);die;
        $payTotalRes=model('TpCart')->payTotalInfo($recId);
        $this->assign([
            'payGoodsRes'=>$payGoodsRes,
            'payTotalRes'=>$payTotalRes,
            'recId'=>$recId,//购物车付款商品信息的key字符串
        ]);

        }
        return view();
    }


    public function flow3(){

        $data=input('post.');
        // dump($data);die;
        //收货地址表
        $addressDB=db('tp_address');
        $uid= session('uid');
        $adData['consignee']=input('consignee');
        $adData['phone']=input('phone');
        $adData['province']=input('province');
        $adData['city']=input('city');
        $adData['country']=input('country');
        $adData['address']=input('address');
        $adData['email']=input('email');
        $adData['zipcode']=input('zipcode');
        $adData['sign_building']=input('sign_building');
        $adData['best_time']=input('best_time');
        $adData['user_id']=$uid;
        $address=$addressDB->where('user_id',$uid)->find();//如果存在就更新，不存在就插入
        if($address){
            $addressDB->where('user_id',$uid)->update($adData);
        }else{
            $addressDB->insert($adData);
        }
        //订单信息表
        
        $orData['out_trade_no']=time().rand(111111,999999);
        $orData['user_id']=$uid;
        $orData['post_spent']=10;//运费接口
        $orData['goods_total_price']=input('goods_total_price');
        $orData['order_tptal_price']=input('goods_total_price')+$orData['post_spent'];//订单金额等于商品金额+运费（默认为0）
        $orData['payment']=input('payment');
        $orData['distribution']=input('distribution');
        $orData['consignee']=input('consignee');
        $orData['phone']=input('phone');
        $orData['province']=input('province');
        $orData['city']=input('city');
        $orData['country']=input('country');
        $orData['address']=input('address');
        $orData['order_time']=time();
        $orderId=db('tp_order')->insertGetId($orData);

        //商品订单表
        $recId=$data['cart_value'];//选中商品key的字符串
        $payGoodsRes=model('TpCart')->payGoodsInfo($recId);
        foreach ($payGoodsRes as $k => $v) {
            $goodsOrData['goods_id']=$v['goods_id'];
            $goodsOrData['goods_name']=$v['goods_name'];
            $goodsOrData['goods_price']=$v['member_price'];
            $attrId=explode('-',$v['goodsId_attrIds']);
            $goodsOrData['goods_attr_id']= $attrId[1];
            $goodsOrData['goods_attr_str']=$v['goods_attr_str'];
            $goodsOrData['goods_num']=$v['number'];
            $goodsOrData['order_id']=$orderId;
            $goodsOrData['item_no']=time().rand(111111,999999);
            db('tp_order_goods')->insert($goodsOrData);

        }
       $this->success('提交订单成功',url('index/Flow/flow4',array('orderId'=>$orderId)));  
           
    
    }

    public function flow4($orderId){
        $orderInfo=db('tp_order')->find($orderId);
        $this->assign([
            'orderInfo'=>$orderInfo
        ]);
        return view();
    }


//支付宝支付成功同步跳转
    public function paySuccess(){
        $arr=input('get.');
        
        $outTradeNo = $arr['out_trade_no'];
        $orderInfo = db('tp_order')->where('out_trade_no',$outTradeNo)->find();
        $this->assign([
            'orderInfo'=>$orderInfo
            ]);
        
        return view();
    }


//支付宝成功异步通知
    public function notifyUrl(){
        $orderDB=db('tp_order');
    include(PAY.'alipay/notify_url.php');
    }

//更新商品数量
    public function ajaxUpdateCart(){
        $data=input('post.');
        model('TpCart')->UpdateCart($data['rec_id'],$data['goods_number']);
        $result=array('error'=>0,'goods_number'=>$data['goods_number'],'rec_id'=>$data['rec_id'],'cart_code'=>$data['cart_code'],'url'=>'flow1');
        return json($result);
    }



//计算选中商品的价格
    public function ajaxCartGoodsAmount(){
        $data=input('post.');
        $recId=$data['rec_id'];
        return model('TpCart')->ajaxCartGoodsAmount($recId);

    }


   //删除购物车的一条记录
    public function delCartGoods($idAttr){
       $del=model('TpCart')->delCartGoods($idAttr);
       if($del){
         $this->redirect('index/Flow/flow1');
       }  
    }


//批量删除购物车的记录
    public function delCart(){
        $cartValue=input('cart_value');//格式：25-51,53@25-50,53@25-50,54
        $del=model('TpCart')->delCart($cartValue);
        if($del){
            $data=array('error'=>0,'msg'=>'删除成功');
            return json($data);
        }   
    }

//获取购物车商品的数量
    public function getCartNum(){
        $CartNum=0;
        $cart=isset($_COOKIE['cart'])?unserialize($_COOKIE['cart']):array();
        foreach ($cart as $k => $v) {
           $CartNum+=$v;
        }
        return  json($CartNum);
    }

    //购物车未登录结算弹出登录框(未完成)
    public function ajaxIsLogin(){
        return 1;

    }

}
